package com.buanalintas.sandals.pos.paymentshop;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosTransactionDistribution;
import com.buanalintas.sandals.pos.entities.PosTransactionPaymentShop;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PaymentShop extends Page {
    
    private Field paymentShopId;
    private Field paymentShopUserId;
    private Field paymentShopUnitId;
    private Field paymentShopDistributionId;
    private Field paymentShopDate;
    private Field paymentShopTotal;
    private Field paymentShopPaid;
    private Field paymentShopKeel;
    private Field paymentShopPay;
    private Field paymentShopTimestamp;
    private Field paymentShopDescription;

    public PaymentShop() {
        setTheClass(PosTransactionPaymentShop.class);

        paymentShopId = new Field();
        paymentShopId.setName("paymentShopId");
        paymentShopId.setCaption("id");
        paymentShopId.setListProperties("");
        paymentShopId.setInputProperties("");
        getFields().add(paymentShopId);

        paymentShopUserId = new Field();
        paymentShopUserId.setName("paymentShopUserId");
        paymentShopUserId.setCaption("user");
        paymentShopUserId.setListProperties("");
        paymentShopUserId.setInputProperties(" size=\"20\" readonly");
        getFields().add(paymentShopUserId);

        paymentShopUnitId = new Field();
        paymentShopUnitId.setName("paymentShopUnitId");
        paymentShopUnitId.setCaption("unit");
        paymentShopUnitId.setListProperties("");
        paymentShopUnitId.setInputProperties(" size=\"20\" readonly");
        getFields().add(paymentShopUnitId);

        paymentShopDistributionId = new Field();
        paymentShopDistributionId.setName("paymentShopDistributionId");
        paymentShopDistributionId.setCaption("payment distribution");
        paymentShopDistributionId.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        paymentShopDistributionId.setInputProperties("");
        paymentShopDistributionId.setRequired(true);
        paymentShopDistributionId.setSelectorCondition("t.distributionKeel='1' AND t.distributionProgress='4'");
        paymentShopDistributionId.getSelectorAction().add(new String[]{"value", "paymentShopTotal", "distributionTotal"});
        paymentShopDistributionId.getSelectorAction().add(new String[]{"value", "paymentShopPaid", "distributionPaid"});
        paymentShopDistributionId.getSelectorColumns().add(new String[]{"distributionNumber", "Number", "width=\"10%\" nowrap"});
        paymentShopDistributionId.getSelectorColumns().add(new String[]{"distributionTotal", "Total", " align=\"right\" width=\"10%\" nowrap"});
        paymentShopDistributionId.getSelectorColumns().add(new String[]{"distributionPaid", " Paid", " align=\"right\" width=\"10%\" nowrap"});
        paymentShopDistributionId.getSelectorColumns().add(new String[]{"distributionDescription", " Description", "width=\"70%\""});
        paymentShopDistributionId.setSelectorOrder("t.distributionNumber");
        getFields().add(paymentShopDistributionId);

        paymentShopDate = new Field();
        paymentShopDate.setName("paymentShopDate");
        paymentShopDate.setCaption("date");
        paymentShopDate.setListProperties(" align=\"center\" width=\"8%\" nowrap");
        paymentShopDate.setInputProperties(" size=\"8\" readonly");
        paymentShopDate.setRequired(true);
        paymentShopDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(paymentShopDate);

        paymentShopTotal = new Field();
        paymentShopTotal.setName("paymentShopTotal");
        paymentShopTotal.setCaption("total");
        paymentShopTotal.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        paymentShopTotal.setInputProperties(" size=\"8\" readonly");
        paymentShopTotal.setRequired(true);
        paymentShopTotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentShopTotal);

        paymentShopPaid = new Field();
        paymentShopPaid.setName("paymentShopPaid");
        paymentShopPaid.setCaption("paid");
        paymentShopPaid.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentShopPaid.setInputProperties(" size=\"8\" readonly");
        paymentShopPaid.setRequired(true);
        paymentShopPaid.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentShopPaid);

        paymentShopKeel = new Field();
        paymentShopKeel.setName("paymentShopKeel");
        paymentShopKeel.setCaption("keel");
        paymentShopKeel.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentShopKeel.setInputProperties(" size=\"8\"");
        paymentShopKeel.getOptions().add(new String[]{"1", "No"});
        paymentShopKeel.getOptions().add(new String[]{"2", "Yes"});
        getFields().add(paymentShopKeel);

        paymentShopPay = new Field();
        paymentShopPay.setName("paymentShopPay");
        paymentShopPay.setCaption("pay");
        paymentShopPay.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentShopPay.setInputProperties(" size=\"8\" ");
        paymentShopPay.setRequired(true);
        paymentShopPay.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentShopPay);

        paymentShopTimestamp = new Field();
        paymentShopTimestamp.setName("paymentShopTimestamp");
        paymentShopTimestamp.setCaption("timestamp");
        paymentShopTimestamp.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        paymentShopTimestamp.setInputProperties(" size=\"8\" readonly");
        paymentShopTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(paymentShopTimestamp);

        paymentShopDescription = new Field();
        paymentShopDescription.setName("paymentShopDescription");
        paymentShopDescription.setCaption("description");
        paymentShopDescription.setListProperties(" align=\"left\" width=\"40%\" nowrap");
        paymentShopDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        paymentShopDescription.setInputType(Field.INPUT_TEXT_AREA);
        paymentShopDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(paymentShopDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(paymentShopDate);
        getFieldsAdd().add(paymentShopDistributionId);
        getFieldsAdd().add(paymentShopTotal);
        getFieldsAdd().add(paymentShopPaid);
        getFieldsAdd().add(paymentShopPay);
        getFieldsAdd().add(paymentShopDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(paymentShopDate);
        getFieldsEdit().add(paymentShopDistributionId);
        getFieldsEdit().add(paymentShopTotal);
        getFieldsEdit().add(paymentShopPay);
        getFieldsEdit().add(paymentShopPaid);
        getFieldsEdit().add(paymentShopDescription);

        getFieldsList().clear();
        getFieldsList().add(paymentShopDistributionId);
        getFieldsList().add(paymentShopDate);
        getFieldsList().add(paymentShopTotal);
        getFieldsList().add(paymentShopPay);
        getFieldsList().add(paymentShopPaid);
        getFieldsList().add(paymentShopKeel);
        getFieldsList().add(paymentShopDescription);

        getFieldsView().clear();
        getFieldsView().add(paymentShopDistributionId);
        getFieldsView().add(paymentShopUserId);
        getFieldsView().add(paymentShopUnitId);
        getFieldsView().add(paymentShopTotal);
        getFieldsView().add(paymentShopPaid);
        getFieldsView().add(paymentShopKeel);
        getFieldsView().add(paymentShopTimestamp);
        getFieldsView().add(paymentShopDescription);

        getActions().remove(Action.EDIT);
        getActions().remove(Action.DELETE);
        getActions().remove(Action.DELETE_MULTI);

        setController(new PaymentShopController(this));
        getController().setOrder(paymentShopTimestamp);
        getController().setOrderType(Controller.ORDER_DESC);
        
        setExportList(getClass().getResourceAsStream("PaymentShopList.jasper"));
        setExport(getClass().getResourceAsStream("PaymentShop.jasper"));
    }

    @Override
    public String validate(Object data) {
        EntityManager em = getEntityManager();
        PosTransactionPaymentShop paymentShop = (PosTransactionPaymentShop) data;
        BigDecimal remaining = paymentShop.getPaymentShopDistributionId().getDistributionTotal().subtract(paymentShop.getPaymentShopDistributionId().getDistributionPaid());
        if (paymentShop.getPaymentShopPay().compareTo(remaining) > 0) {
            MoneyFormatter formatter = MoneyFormatter.getInstance();
            return paymentShop.getPaymentShopDistributionId() + " " + Dictionary.getValue(this, "Too much pay") + ", " + Dictionary.getValue(this, "total bill") + ": " + formatter.format(remaining) + " " + Dictionary.getValue(this, "money you pay now") + ": " + formatter.format(paymentShop.getPaymentShopPay());
        }
        return null;
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        paymentShopDate.setFormater(DateFormatter.getInstanceDate(this));
        paymentShopTimestamp.setFormater(DateFormatter.getInstanceDateTime(this));
    }

    class PaymentShopController extends Controller {
        public PaymentShopController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            PosTransactionPaymentShop paymentShop = (PosTransactionPaymentShop) data;
            paymentShop.setPaymentShopUnitId(getCurrentUnit());
            paymentShop.setPaymentShopUserId(getCurrentUser());
            paymentShop.setPaymentShopTimestamp(Utility.getTimestamp());
            paymentShop.setPaymentShopPaid(paymentShop.getPaymentShopDistributionId().getDistributionPaid().add(paymentShop.getPaymentShopPay()));
            paymentShop.getPaymentShopDistributionId().setDistributionPaid(paymentShop.getPaymentShopPaid());
            if (paymentShop.getPaymentShopDistributionId().getDistributionTotal().compareTo(paymentShop.getPaymentShopDistributionId().getDistributionPaid()) == 0) {
                paymentShop.setPaymentShopKeel("2");
                paymentShop.getPaymentShopDistributionId().setDistributionKeel("2");
            } else {
                paymentShop.setPaymentShopKeel("1");
            }
        }
    }

    @Override
    public void loadDetail(List datas, String id) {
        EntityManager em = getEntityManager();
        List<PosTransactionPaymentShop> request = em.createQuery("SELECT t FROM PosTransactionDistribution t WHERE t.distributionDetailId.distributionId='" + id + "'").setHint(Persistence.REFRESH_HINT, true).getResultList();
        datas.clear();
        PosTransactionDistribution distribution = (PosTransactionDistribution) request;
        PosTransactionPaymentShop paymentShop = new PosTransactionPaymentShop();
        paymentShop.setPaymentShopTotal(distribution.getDistributionTotal());
        paymentShop.setPaymentShopDescription(distribution.getDistributionDescription());
        datas.add(paymentShop);
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Payment Shop"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
